<?php

/**
 * Class that operate on table 'users'. Database Sqlite.
 *
 * @author: Dragan Milchevski
 * @date: 2012-06-24 03:35
 */
class UsersSqliteDAO implements UsersDAO {

    /**
     * Get Domain object by primry key
     *
     * @param String $id primary key
     * @return UsersSqlite 
     */
    public function load($id) {
        $sql = 'SELECT * FROM users WHERE userID = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->setNumber($id);
        return $this->getRow($sqlQuery);
    }

    /**
     * Get all records from table
     */
    public function queryAll() {
        $sql = 'SELECT * FROM users';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    /**
     * Get all records from table ordered by field
     *
     * @param $orderColumn column name
     */
    public function queryAllOrderBy($orderColumn) {
        $sql = 'SELECT * FROM users ORDER BY ' . $orderColumn;
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    /**
     * Delete record from table
     * @param user primary key
     */
    public function delete($userID) {
        $sql = 'DELETE FROM users WHERE userID = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->setNumber($userID);
        return $this->executeUpdate($sqlQuery);
    }

    /**
     * Insert record to table
     *
     * @param UsersSqlite user
     */
    public function insert($user) {
        $sql = 'INSERT INTO users (username, password, permissions) VALUES (?, ?, ?)';
        $sqlQuery = new SqlQuery($sql);

        $sqlQuery->set($user->getUsername());
        $sqlQuery->set($user->getPassword());
        $sqlQuery->set($user->getPermissions());

        $id = $this->executeInsert($sqlQuery);
        $user->setId($id);
        return $id;
    }

    /**
     * Update record in table
     *
     * @param UsersSqlite user
     */
    public function update($user) {
        $sql = 'UPDATE users SET username = ?, password = ?, permissions = ? WHERE userID = ?';
        $sqlQuery = new SqlQuery($sql);

        $sqlQuery->set($user->getUsername());
        $sqlQuery->set($user->getPassword());
        $sqlQuery->set($user->getPermissions());

        $sqlQuery->setNumber($user->getId());
        return $this->executeUpdate($sqlQuery);
    }

    /**
     * Delete all rows
     */
    public function clean() {
        $sql = 'DELETE FROM users';
        $sqlQuery = new SqlQuery($sql);
        return $this->executeUpdate($sqlQuery);
    }

    public function queryByUsername($value) {
        $sql = "SELECT * FROM users WHERE username = ?";
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);		
        return $this->getRow($sqlQuery);
    }

    public function queryByPassword($value) {
        $sql = 'SELECT * FROM users WHERE password = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);
        return $this->getList($sqlQuery);
    }

    public function queryByPermissions($value) {
        $sql = 'SELECT * FROM users WHERE permissions = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);
        return $this->getList($sqlQuery);
    }

    public function deleteByUsername($value) {
        $sql = 'DELETE FROM users WHERE text = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);
        return $this->executeUpdate($sqlQuery);
    }

    public function deleteByPassword($value) {
        $sql = 'DELETE FROM users WHERE text = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);
        return $this->executeUpdate($sqlQuery);
    }

    public function deleteByPermissions($value) {
        $sql = 'DELETE FROM users WHERE text = ?';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->set($value);
        return $this->executeUpdate($sqlQuery);
    }

    /**
     * Read row
     *
     * @return UsersSqlite 
     */
    protected function readRow($row) {
        $user = new User();

        $user->setId($row['userID']);
        $user->setUsername($row['username']);
        $user->setPassword($row['password']);
        $user->setPermissions($row['permissions']);

        return $user;
    }

    protected function getList($sqlQuery) {
        $tab = QueryExecutor::execute($sqlQuery);
        $ret = array();
        for ($i = 0; $i < count($tab); $i++) {
            $ret[$i] = $this->readRow($tab[$i]);
        }
        return $ret;
    }

    /**
     * Get row
     *
     * @return UsersSqlite 
     */
    protected function getRow($sqlQuery) {
        $tab = QueryExecutor::execute($sqlQuery);
        if (count($tab) == 0) {
            return null;
        }
        return $this->readRow($tab[0]);
    }

    /**
     * Execute sql query
     */
    protected function execute($sqlQuery) {
        return QueryExecutor::execute($sqlQuery);
    }

    /**
     * Execute sql query
     */
    protected function executeUpdate($sqlQuery) {
        return QueryExecutor::executeUpdate($sqlQuery);
    }

    /**
     * Query for one row and one column
     */
    protected function querySingleResult($sqlQuery) {
        return QueryExecutor::queryForString($sqlQuery);
    }

    /**
     * Insert row to table
     */
    protected function executeInsert($sqlQuery) {
        return QueryExecutor::executeInsert($sqlQuery);
    }

}

?>